<template>
  <div class="head">
    <el-row>
      <el-col :span="2" :offset="22">
        <div class="grid-content bg-purple">
          <el-popover
            placement="top-start"
            width="200"
            trigger="hover"
            >
            <div>
              <template  v-for="(item, index) in cars"  :body-style="{ padding: '0px' }">
                <div :key="index" class="car-main">
                  <img :src="item.mainUrl" style="width:80px;height: 80px">
                  <div style="padding: 14px;">
                    <span>{{item.name}}</span>
                    <div class="bottom clearfix">
                      <time class="time">￥{{ item.price }}</time>
                    </div>
                  </div>
                </div>
              </template>
              <el-button size="mini" style="margin-top:10px" @click="buyBtn">去购买</el-button>
            </div>
            <!-- <el-button></el-button> -->
            <i  slot="reference" class="el-icon-shopping-cart-full"></i>
          </el-popover>
          <el-dropdown>
            <span class="el-dropdown-link">
            <i class="el-icon-user el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item><router-link to="/login"><el-button size="mini">退出登录</el-button></router-link></el-dropdown-item>
              <el-dropdown-item><el-button size="mini" @click="dialogVisible = true">修改密码</el-button></el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-col>
    </el-row>
    <el-dialog
      title="修改密码"
      :visible.sync="dialogVisible"
      width="500px"
      :before-close="handleClose">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="100px" class="demo-ruleForm">
        <el-form-item label="旧密码" prop="oldPassword">
          <el-input type="password" v-model="ruleForm.oldPassword" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input type="password" v-model="ruleForm.newPassword" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updatePw">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapState, mapActions } from "vuex";
import { utilsMessage } from '../utils/utils';
import{updatePasswoer} from '../api/index';
import {getCar} from '../api/index';
export default {
  data(){
    return {
      currentDate: new Date(),
      dialogVisible: false,
      ruleForm: {

      },
      rules: {
        oldPassword: [ { required: true, message: '请输入旧密码', trigger: 'blur' } ],
        newPassword: [ { required: true, message: '请输入新密码', trigger: 'blur' } ],
      }
    }
  },
  computed:{
    ...mapState({
      cars: state => state.cars
    })
  },
  methods:{
    buyBtn(){
      if(this.$store.state.memberId){
        this.$router.push('/cart')
        // console.log(this.$store.state.memberId)
      }else{
        // console.log('没有登录')
        utilsMessage('请登录');
        this.$router.push('/login')
      }
    },
    handleClose(){
      this.dialogVisible = false
    },
    updatePw(){
      this.$refs['formName'].validate((valid) => {
        if (valid) {
          updatePasswoer({id: this.$store.state.memberId,...this.ruleForm}).then(res=>{
            if(res.data){
              utilsMessage(res.msg);
              this.dialogVisible = false;
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  },
  mounted(){
    getCar({memberId: this.$store.state.memberId}).then(res=>{
      // console.log(res)
      this.$store.commit('setCars',res.data);
    })
    // console.log(this.cars)
  }
}
</script>
<style lang="scss">
.head{
  width: 100%;
  height: 50px;
  line-height: 50px;
  background: black;
  .left{
    height: 50px;
  }
  i{
    font-size: 16px;
    color: white;
    padding: 10px;
  }
  
}
.car-main{
    width: 200px;
    height: 80px;
    display: flex;
  }
</style>